<--- %%NOBANNER%% --> excelclose.sas
 BackForward

/*-------------------<---Start of Description-->---------------------\
| Close and exit the current excel spread sheet.                     |
|---------------------<---End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<---Start of Files or Arguments Needed-->---------------|
| Argments:                                                          |
|    excelref: the excel reference; not necessary; default is        |
|              "excelsys";                                           |
| Make sure an excel sheet is opened, otherwise it will generate an  |
| error message in the log;                                          |
|-------------<---End of Files or Arguments Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<---Start of Files Created-->---------------------|
| The opened excel sheet will be close;                              |
| Usage: %excelclose(excelref, win=excel);                           |
\-------------------<---End of Files Created-->---------------------*/
%macro excelclose/parmbuff;
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  6-15-2001 8:22pm;                 |
| Modified: 1-15-2001 7:28pm;                 |
| Purpose:  Close the excel spread sheet;     |
\--------------------------------------------*/
%local arg fref wsid;
%let arg=%qscan(&syspbuff,1,%str((),)); 
%let arg=%sysfunc(dequote(&arg));
%let fref=; %let wsid=0;
%if (%index(%quote(%upcase(%sysfunc(compress(%quote(&arg))))),%str(EXCELREF=))) or 
    (%index(%quote(%upcase(%sysfunc(compress(%quote(&arg))))),%str(WINREF=))) or 
    (%index(%quote(%upcase(%sysfunc(compress(%quote(&arg))))),%str(WINDOW=))) or
    (%index(%quote(%upcase(%sysfunc(compress(%quote(&arg))))),%str(WIN=)))
      %then %do;
   %let fref=%qscan(&arg, 2, %str(= ));
   %put --> Note: Checking the status of window "&fref" to see if it is opened.;
%end;
%else %if (%quote(&arg) ne) %then %do;
   %if not (%index(%quote(%upcase(%sysfunc(compress(%quote(&arg))))),%str(=))) %then %do; 
      %if (%index(%quote(&arg), %quote(\))) or (%index(%quote(&arg), %quote(/))) %then %do;
         %put --> Alert! This is not a valid window reference "&arg".;
      %end;
      %else %do; %let fref=&arg; %put --> Note: Checking the status of window "&fref"...; %end;
   %end;
   %else %do;
      %put --> Alert! Keyword Parameter isn%str(%')t declared!;
   %end;
%end;
%else %if (%quote(&arg) eq) %then %do;
   %let fref=excelsys;
   %put --> Note: No window reference is specified%str(;) Checking the status of the default; 
   %put -->       "&fref" to see if it is open.;
%end;
%if (%quote(&fref) ne) %then %do;
   %let wsid=%sysfunc(fopen(&fref,s));
%end;
%if &wsid %then %do;   
   %let rc=%sysfunc(fclose(&wsid)); 
   Data _null_;
      FILE &fref;
      PUT '[CLOSE]';
      PUT '[QUIT]';
   run;
%end;
%else %put ==> Alert! Incorrect window reference "&fref", or window "&fref" isn%str(%')t open.;
%mend excelclose;